﻿@model int?

@using SmartStore.Web.Framework.UI;
@using SmartStore.Utilities;
@using SmartStore.Core.Domain.Media;

@functions {
	bool? _minimalMode = null;
	string _fieldName = null;
	int _entityId = 0;
	string _entityName = null;
	string _fileTypes = null;
	string _path = "";

	private bool MinimalMode
	{
		get
		{
			if (!_minimalMode.HasValue)
			{
				_minimalMode = ViewData.ContainsKey("minimalMode") ? ViewData["minimalMode"].Convert<bool>() : false;
			}
			return _minimalMode.Value;
		}
	}

	private string FieldName
	{
		get
		{
			if (_fieldName == null)
			{
				_fieldName = ViewData.ContainsKey("fieldName") ? ViewData["fieldName"].Convert<string>() : ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty);
			}
			return _fieldName;
		}
	}

	private int EntityId
	{
		get
		{
			if (_entityId == 0)
			{
				_entityId = ViewData.ContainsKey("entityId") ? ViewData["entityId"].Convert<int>() : 0;
			}
			return _entityId;
		}
	}

	private string EntityName
	{
		get
		{
			if (_entityName == null)
			{
				_entityName = ViewData.ContainsKey("entityName") ? ViewData["entityName"].Convert<string>() : ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty);
			}
			return _entityName;
		}
	}

	private string FileTypes
	{
		get
		{
			if (_fileTypes == null)
			{
				_fileTypes = ViewData.ContainsKey("fileTypes") ? ViewData["fileTypes"].Convert<string>() : "";
			}
			return _fileTypes;
		}
	}

	private string Path
	{
		get
		{
			if (!_path.HasValue())
			{
				_path = ViewData.ContainsKey("path") ? ViewData["path"].Convert<string>() : "download";
			}
			return _path;
		}
	}
}

@{
	var random = CommonHelper.GenerateRandomInteger();
	var clientId = "download-editor-" + random;
	var mediaService = this.CommonServices.MediaService;
	var mediaSettings = this.CommonServices.Resolve<MediaSettings>();
	var downloadService = EngineContext.Current.Resolve<SmartStore.Services.Media.IDownloadService>();
	var download = downloadService.GetDownloadById(Model.GetValueOrDefault());
	var fileName = string.Empty;
	var initiallyShowUrlPanel = false;
	var hasFile = false;
	var downloadUrl = "";
	var thumbUrl = "";
	if (download != null)
	{
		downloadUrl = Url.Action("DownloadFile", "Download", new { downloadId = download.Id });
		initiallyShowUrlPanel = !MinimalMode && download.UseDownloadUrl;
		hasFile = !download.UseDownloadUrl;
		fileName = download.MediaFile?.Name ?? (download.Id.ToString().Grow(download.MediaFile?.Extension, "."));
		thumbUrl = Url.Media(download.MediaFileId, mediaSettings.ProductThumbPictureSize);
	}

	Html.AddScriptParts(true, "~/Administration/Scripts/smartstore.download.js");
}

<div id="@clientId" class="form-row flex-nowrap download-editor@(MinimalMode ? " minimal" : "")"
	 data-download-id="@Model.GetValueOrDefault()"
	 data-delete-url="@Url.Action("DeleteDownload", "Download", new { minimalMode = MinimalMode, fieldName = FieldName, downloadId = Model, area = "Admin" })"
	 data-use-url="@(initiallyShowUrlPanel ? "true" : "false")">

	<div @Html.Attr("class", "col-auto", !MinimalMode || hasFile)>
		<!-- Type Chooser Dropdown -->
		@if (!MinimalMode)
		{
			<div class="btn-group panel-switcher" title='@T("Admin.Download.UploadFile")'>
				<a class="btn btn-secondary btn-icon dropdown-toggle" data-toggle="dropdown" href="#">
					<i class="fa fa-@(initiallyShowUrlPanel ? "globe": "upload") panel-switcher-icon"></i>
				</a>
				<div class="dropdown-menu">
					<a href="#" class="dropdown-item toggle-file@(!initiallyShowUrlPanel ? " disabled" : "")">@T("Admin.Download.UploadFile")</a>
					<a href="#" class="dropdown-item toggle-url@(initiallyShowUrlPanel ? " disabled" : "")">@T("Common.FileUploader.EnterUrl")</a>
					@if (download != null)
					{
						<div class="dropdown-divider"></div>
						<a href="#" class="dropdown-item remove-download">
							<i class="far fa-trash-alt"></i>
							<span>@T("Common.Remove")</span>
						</a>
					}
				</div>
			</div>
		}
	</div>

	<!-- Main Panel -->
	<div class="col panel-upload fu-container">

		<!-- FileUpload Panel  -->
		<div class="form-row flex-nowrap dropzone-container panel-file@(initiallyShowUrlPanel ? " hide": "")">

			<div class="fu-thumb" data-debug-info="@(download != null ? download.MediaFileId : 0)">
				<img class="file-img" title="icon" src="@thumbUrl">
			</div>

			<input type="hidden" id="@FieldName.SanitizeHtmlId()" name="@FieldName" value="@Model" class="hidden" />

			<div class="fu-controls">
				@(Html.SmartStore().FileUploader()
					.UploadUrl(Url.Action("AsyncUpload", "Download", new { minimalMode = MinimalMode, fieldName = FieldName, entityId = EntityId, entityName = EntityName, area = "Admin" }))
					.DeleteUrl(Url.Action("DeleteDownload", "Download", new { minimalMode = MinimalMode, fieldName = FieldName, downloadId = Model, area = "Admin" }))
					.Multifile(false)
					.Path(Path)
					.UploadText(hasFile ? T("Common.Replace").Text : T("Common.Fileuploader.Upload").Text)
					.HasTemplatePreview(true)
					.ShowRemoveButton(false)
					.ShowRemoveButtonAfterUpload(false)
					.OnMediaSelectedHandlerName("onMediaSelected_" + random)
				)
			</div>
		</div>

		<!-- URL Panel -->
		@if (!MinimalMode)
		{
			<div class="panel-url" style="display: @(initiallyShowUrlPanel ? "block": "none")">
				<div class="input-group">
					@{ var value = download != null ? download.DownloadUrl : ""; }
					<input type="text" class="download-url-value form-control" value='@value' data-value='@value' />
					<div class="input-group-append">
						<a class="btn btn-outline-secondary save-download-url disabled" href="@Url.Action("SaveDownloadUrl", "Download", new { minimalMode = MinimalMode, fieldName = FieldName, entityId = EntityId, entityName = EntityName, area = "Admin" })">
							<i class="fa fa-check"></i>
							<span>@T("Common.Save")</span>
						</a>
					</div>
				</div>
			</div>
		}

		<div class="fu-progress">
			<!-- The single upload progress bar -->
			<div class="progress">
				<div class="progress-bar" role="progressbar" style="width: 0%" aria-valuemin="0" aria-valuemax="100"></div>
			</div>
		</div>
	</div>
</div>

<script>
    window['onMediaSelected_@random'] = function (file) {
		// Create download for selected MediaFile.
		$.ajax({
			async: true,
			cache: false,
			type: 'POST',
			url: "@Url.Action("CreateDownloadFromMediaFile", "Download")",
			data: {
				mediaFileId: file.id,
                entityId: @EntityId,
				entityName: "@EntityName"
			},
			success: function (response) {
                if (!response.success)
                    displayNotification("Error when trying to create a download for the selected file.", "error");
			}
		});
    }

	$(function () {
		$('#@clientId').downloadEditor();
	});
</script>